package vue.stage;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import metier.CategorieStage;
import ctr.commun.CommunGest;
import ctr.stage.GestAjoutStage;

public class JPanAjoutStage extends JPanel {

	private JComboBox jcb_NomCategorie;
	private JTextField jtf_NomAnnee, jtf_DateDeb, jtf_DateFin;
	private static final long serialVersionUID = -8258905260255616984L;

	public JPanAjoutStage(String titre,String str_Annee,String str_dateDeb,String str_dateFin) {
		// Label

		JPanel panelLabel = new JPanel();
		panelLabel.setLayout(new GridLayout(4, 1, 5, 5));

		JLabel lbl_NomCategorie = new JLabel("Categorie : ");
		JLabel lbl_annee = new JLabel("Année");
		JLabel lbl_dateDeb = new JLabel("Date de début");
		JLabel lbl_dateFin = new JLabel("Date de fin");

		panelLabel.add(lbl_annee);
		panelLabel.add(lbl_NomCategorie);
		panelLabel.add(lbl_dateDeb);
		panelLabel.add(lbl_dateFin);

		// Champ de saisie

		JPanel panelSaisi = new JPanel();
		panelSaisi.setLayout(new GridLayout(4, 1, 5, 5));

		jtf_NomAnnee = new JTextField(5);
		if (str_Annee!=null) {
			jtf_NomAnnee.setText(str_Annee);
		}else {
			Calendar c = Calendar.getInstance();
			  int year = c.get(Calendar.YEAR);
			jtf_NomAnnee.setText(""+year);
		}
		
		jtf_NomAnnee.addActionListener(new GestAjoutStage(this));
		jtf_DateDeb = new JTextField(15);
		
		if (str_dateDeb!=null) {
			jtf_DateDeb.setText(str_dateDeb);
		}
		jtf_DateDeb.addActionListener(new GestAjoutStage(this));

		jtf_DateFin = new JTextField(15);
		if (str_dateFin!=null) {
			jtf_DateDeb.setText(str_dateFin);
		}
		jtf_DateFin.addActionListener(new GestAjoutStage(this));

		// comboBox
		// Recuperation de la liste des Categories
		CommunGest communGest = new CommunGest();
		ArrayList<CategorieStage> lCategorie = communGest.getListeCategorieStage();

		jcb_NomCategorie = new JComboBox(lCategorie.toArray());

		panelSaisi.add(jtf_NomAnnee);
		panelSaisi.add(jcb_NomCategorie);
		panelSaisi.add(jtf_DateDeb);
		panelSaisi.add(jtf_DateFin);

		// Bouton
		JButton button = new JButton("Ajouter");
		button.addActionListener(new GestAjoutStage(this));

		JPanel panelBouton = new JPanel();
		panelBouton.add(button);

		/*
		 * // Ajout d'un titre et d'un layout this.setBorder(new
		 * TitledBorder(titre)); this.setLayout(new FlowLayout());
		 * 
		 * // Ajout des differents composant. this.add(lbl_annee);
		 * this.add(jtf_NomAnnee); this.add(lbl_NomCategorie);
		 * this.add(jcb_NomCategorie);
		 * 
		 * this.add(lbl_dateDeb); this.add(jtf_DateDeb); this.add(lbl_dateFin);
		 * this.add(jtf_DateFin); this.add(button);
		 */

		// Panel global
		// Avec mise en forme.
		this.setBorder(new TitledBorder(titre));
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.VERTICAL;
		this.add(panelLabel, gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.VERTICAL;
		this.add(panelSaisi, gbc);

		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridheight = 2;
		gbc.fill = GridBagConstraints.CENTER;
		this.add(panelBouton, gbc);

	}
	
	

	public JTextField getJtf_DateDeb() {
		return jtf_DateDeb;
	}



	public JTextField getJtf_DateFin() {
		return jtf_DateFin;
	}



	public JComboBox getJcb_NomCategorie() {
		return jcb_NomCategorie;
	}

	public JTextField getJtf_NomAnnee() {
		return jtf_NomAnnee;
	}

}
